package wjx.database.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.Box;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import wjx.database.bean.JTableUtils;
import wjx.database.jdbc.JDBCUtils;

public class CreateTableFrm extends JFrame {

	private JPanel contentPane;
	private Box boxHead, boxTitle, boxTail, box;
	private JTextField[] textField;
	private JCheckBox[] chkBox1;
	private JCheckBox[] chkBox2;
	private JCheckBox[] chkBox3;
	private JComboBox[] comboBox;
	private JTable table;
	private DefaultTableModel model;
	private JScrollPane scroll;

	private String[] select = null;
	private String[] name = null;
	private int i;
	private int table_counts;
	private String table_name;
	private String[][] data = null;
	private String[] tableTitle = { "列名", "数据类型(长度)", "主键", "不为空", "是否加密" };
	private Class[] typeArray = { Object.class, Object.class, Boolean.class, Boolean.class, Boolean.class };
//	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CreateTableFrm frame = new CreateTableFrm("oo", 1);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CreateTableFrm(String tb_name, int num) {
		chkBox1 = new JCheckBox[num];
		chkBox2 = new JCheckBox[num];
		chkBox3 = new JCheckBox[num];
		select = new String[num];
		name = new String[num];
		i = 0;
		table_counts = num;
		table_name = tb_name;

		textField = new JTextField[num];
		comboBox = new JComboBox[num];

		setResizable(false);
		setTitle("表定义-表'" + tb_name + "'");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		box = Box.createVerticalBox();
		JLabel lb1 = new JLabel("请补全以下信息以实现建表");
		lb1.setFont(new Font("Lucida Grande", 1, 15));
		JLabel lb2 = new JLabel("注意：若某列需要加密，仅支持VARCHAR类型，且长度至少为24。");
		// 创建可编辑表table
		table = new JTable(num, 5);
		model = new DefaultTableModel(num, 5) {
			public Class getColumnClass(int columnIndex) {
				return typeArray[columnIndex];// 返回每一列的数据类型
			}
		};
		// 设置网格线
		table.setShowHorizontalLines(true);
		table.setShowVerticalLines(true);
		table.setBorder(new EtchedBorder(EtchedBorder.RAISED));
		table.setGridColor(Color.BLACK);
		table.setRowHeight(25);

		model.setColumnIdentifiers(tableTitle);
		table.setModel(model);
//		JTableUtils.setTable(table);
		scroll = new JScrollPane(table);

		// 获得表格的表格列类
		TableColumn tc1 = table.getColumnModel().getColumn(2);
		TableColumn tc2 = table.getColumnModel().getColumn(3);
		TableColumn tc3 = table.getColumnModel().getColumn(4);
		tc1.setCellEditor(new DefaultCellEditor(new JCheckBox()));

		boxHead = Box.createHorizontalBox();
		boxHead.add(lb1);
		boxHead.add(Box.createHorizontalStrut(30));
		box.add(boxHead);
		
		Box bxh = Box.createHorizontalBox();
		bxh.add(lb2);
		bxh.add(Box.createHorizontalStrut(40));
		box.add(bxh);
		box.add(Box.createVerticalStrut(10));
		box.add(scroll);

		JButton btn1 = new JButton("确定建表");
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createTableActionPerformed(e);
			}
		});
		JButton btn2 = new JButton("取   消");
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quitActionperformed(e);
			}
		});

		boxTail = Box.createHorizontalBox();
		boxTail.add(Box.createHorizontalStrut(120));
		boxTail.add(btn2);
		boxTail.add(btn1);
		boxTail.add(Box.createHorizontalStrut(100));
		box.add(boxTail);

		contentPane.add(box);
		this.setLocationRelativeTo(null);
	}

	private void quitActionperformed(ActionEvent e) {
		int result = JOptionPane.showConfirmDialog(null, "是否确认退出？", "退出将取消建表", JOptionPane.YES_NO_OPTION);
		// 退出窗口
		if (result == JOptionPane.YES_NO_OPTION)
			dispose();
	}

	private void createTableActionPerformed(ActionEvent e) {
		String sql = "create table databaseManager." + table_name + " (";
		String[] prKey = new String[table_counts];
		int cnt = 0;
		int c = 0 ;
		// 检查输入是否为空
		for (int i = 0; i < table_counts; i++) {
			if (i > 0)
				sql += ",";
			if (model.getValueAt(i, 0) == null) {
				JOptionPane.showMessageDialog(null, "属性列名不能为空！");
				return;
			}
			if (model.getValueAt(i, 1) == null) {
				JOptionPane.showMessageDialog(null, "数据类型不能为空！");
				return;
			}

			sql += model.getValueAt(i, 0).toString() + " " + model.getValueAt(i, 1).toString();
			if (model.getValueAt(i, 2) != null && (boolean) model.getValueAt(i, 2)) {
				prKey[cnt] = model.getValueAt(i, 0).toString();
				cnt++;
			}
			if (model.getValueAt(i, 3) != null && (boolean) model.getValueAt(i, 3)) {
				sql += " " + "not null ";
			}
			if (cnt == 0) {
				JOptionPane.showMessageDialog(null, "请至少选择一个属性列为主键！");
				return;
			}
			if ( model.getValueAt(i, 4) != null && (boolean) model.getValueAt(i, 4)) {
				if( c == 0 ) {
					sql += "comment 'code'";
					c ++ ;
				}else {
					JOptionPane.showMessageDialog(null, "仅允许对一个属性列进行加密，请重新选择！");
					return;
				}
//				MainFrm.myt.getDe(model.getValueAt(i, 0).toString());
			}
		}
		// 建表操作
		if (cnt > 0) {
			sql += ",primary key (";
			for (int i = 0; i < cnt; i++) {
				if (i > 0)
					sql += ",";
				sql += prKey[i];
			}
			sql += ")";

		}
		sql += ")";
		System.out.println(sql);
		int result = -1;
		try {
			result = JDBCUtils.create(sql);
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, "语法错误，请检查您的输入！");
			return;
		}

		// 提示建表成功
		if (result >= 0)
			JOptionPane.showMessageDialog(null, "建表成功！表名为：'" + table_name + "'");
		else
			JOptionPane.showMessageDialog(null, "建表失败！请检查您的输入！");

		// 退出窗口
		dispose();

	}
}
